﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DauGiaApp.Models;

namespace DauGiaApp.Controllers
{
    public class TimKiemController : Controller
    {
        //
        // GET: /TimKiem/
        DauGiaEntities data = new DauGiaEntities();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult KhungTimKiemDonGian()
        {
            ViewData["MaLoaiSanPham"] = new SelectList(data.LoaiSanPhams, "MaLoaiSanPham", "TenLoaiSanPham");
            return PartialView();
        }
        [HttpGet]
        public ActionResult TimKiemDonGian(ThongTinTimKiem sp)
        {
            try
            {
                var dssp = from c in data.SanPhams select c;
                if (!string.IsNullOrWhiteSpace(sp.TenSanPham))
                {
                    dssp = dssp.Where(Idc => Idc.TenSanPham.Contains(sp.TenSanPham));
                }
                if (sp.MaLoaiSanPham != 0)
                {
                    dssp = dssp.Where(IdC => IdC.MaLoaiSanPham == sp.MaLoaiSanPham);
                }
                List<SanPham> listsp = new List<SanPham>();
                if (dssp != null && dssp.Count() > 0)
                {
                    listsp = dssp.ToList();
                    return View(listsp);
                }      
            }
            catch
            {
                return View();
            }
            return View();

        }
        public ActionResult TaoKhungTimKiem()
        {
            ViewData["MaLoaiSanPham"] = new SelectList(data.LoaiSanPhams, "MaLoaiSanPham", "TenLoaiSanPham");
            List<SelectListItem> giatu = new List<SelectListItem>();
            giatu.Add(new SelectListItem { Text = "300,000", Value = "300000" });
            giatu.Add(new SelectListItem { Text = "1,000,000", Value = "1000000" });
            giatu.Add(new SelectListItem { Text = "2,000,000", Value = "2000000" });
            giatu.Add(new SelectListItem { Text = "3,000,000", Value = "3000000" });
            giatu.Add(new SelectListItem { Text = "5,000,000", Value = "5000000" });
            giatu.Add(new SelectListItem { Text = "Trên 10 triệu", Value = "10000000" });
            ViewData["GiaTu"] = new SelectList(giatu, "Value", "Text");
            List<SelectListItem> giaden = new List<SelectListItem>();
            giaden.Add(new SelectListItem { Text = "500,000", Value = "300000" });
            giaden.Add(new SelectListItem { Text = "2,000,000", Value = "1000000" });
            giaden.Add(new SelectListItem { Text = "3,000,000", Value = "2000000" });
            giaden.Add(new SelectListItem { Text = "5,000,000", Value = "5000000" });
            giaden.Add(new SelectListItem { Text = "7,000,000", Value = "7000000" });
            giaden.Add(new SelectListItem { Text = "10 triệu", Value = "10000000" });
            ViewData["GiaDen"] = new SelectList(giaden, "Value", "Text");
            return PartialView();
        }
        public ActionResult ViewTimKiemNangCao()
        {
            return View();
        }
        [HttpGet]
        public ActionResult TimKiemNangCao(ThongTinTimKiem sp)
        {
            try
            {

                var dssp = from c in data.SanPhams
                           select c;
                if (!string.IsNullOrWhiteSpace(sp.TenSanPham))
                {
                    dssp = dssp.Where(IdC => IdC.TenSanPham.Contains(sp.TenSanPham));
                }
                if (sp.MaLoaiSanPham != 0)
                {
                    dssp = dssp.Where(IdC => IdC.MaLoaiSanPham == sp.MaLoaiSanPham);
                }
                if (sp.GiaTu.HasValue)
                {
                    dssp = dssp.Where(IdC => IdC.GiaKhoiDiem > sp.GiaTu);
                }
                if (sp.GiaDen.HasValue)
                {
                    dssp = dssp.Where(IdC => IdC.GiaKhoiDiem < sp.GiaDen);
                }
                if (!string.IsNullOrWhiteSpace(sp.ThoiGianKetThuc.ToString()))
                {
                    dssp = dssp.Where(IdC => ((DateTime)(IdC.NgayHetHan)).Day == ((DateTime)(sp.ThoiGianKetThuc)).Day);
                }
                List<SanPham> listsp = new List<SanPham>();
                if (dssp != null && dssp.Count() > 0)
                {
                    listsp = dssp.ToList();
                    return PartialView(listsp);
                }
            }
            catch
            {
                return PartialView();
            }
            return PartialView();
        }
    }
}